﻿Class uRibbon
    Public RibbonSupressErrors As Boolean

    Public Event ClickedTab(ByVal zID As String, ByVal zTabText As String)
    Public Event ClickedTabGroup(ByVal zID As String, ByVal zTabText As String, ByVal zGroupText As String)
    Public Event ClickedTabGroupPopout(ByVal zID As String, ByVal zTabText As String, ByVal zGroupText As String)
    Public Event ClickedTabGroupButton(ByVal zID As String, ByVal zTabText As String, ByVal zGroupText As String, ByVal zButtonText As String)
    Public Event ClickedTabGroupButtonMenu(ByVal zID As String, ByVal zTabText As String, ByVal zGroupText As String, ByVal zButtonText As String, ByVal zMenuText As String)

    Private mToolTips As New ToolTip
    Private mCurrentTab As String

    Private Structure FadeItem
        Dim FadeTimer As Timer
        Dim FadeIn As Boolean
        Dim FadeSpeed As Single
        Dim FadeAlpha As Single
    End Structure

    Private Structure RibbonMenu
        Dim ID As String
        Dim Text As String
        Dim Picture As Image
    End Structure
    Private Structure RibbonMenus
        Dim Count As Integer
        Dim Details() As RibbonMenu
    End Structure

    Private Structure RibbonButton
        Dim ID As String
        Dim Text As String
        Dim Enabled As Boolean
        Dim Picture As Image
        Dim More As Boolean
        Dim MoreOver As Boolean
        Dim ToolTip As String
        Dim ObjL As PictureBox
        Dim ObjC As PictureBox
        Dim ObjR As PictureBox
        Dim ObjPicture As PictureBox
        Dim ObjText As Label
        Dim ObjMore As PictureBox
        Dim _Menus As RibbonMenus
        Dim Fader As FadeItem
    End Structure
    Private Structure RibbonButtons
        Dim Count As Integer
        Dim Details() As RibbonButton
    End Structure

    Private Structure RibbonGroup
        Dim ID As String
        Dim Text As String
        Dim PopOut As Boolean
        Dim PopOutOver As Boolean
        Dim ToolTip As String
        Dim ObjL As PictureBox
        Dim ObjC As PictureBox
        Dim ObjR As PictureBox
        Dim ObjText As Label
        Dim objPopOut As PictureBox
        Dim _Buttons As RibbonButtons
        Dim Fader As FadeItem
    End Structure
    Private Structure RibbonGroups
        Dim Count As Integer
        Dim Details() As RibbonGroup
    End Structure

    Private Structure RibbonTab
        Dim ID As String
        Dim Text As String
        Dim Enabled As Boolean
        Dim ToolTip As String
        Dim ObjL As PictureBox
        Dim ObjC As PictureBox
        Dim ObjR As PictureBox
        Dim ObjText As Label
        Dim _Groups As RibbonGroups
        Dim Fader As FadeItem
    End Structure
    Private Structure RibbonTabs
        Dim Count As Integer
        Dim Details() As RibbonTab
    End Structure

    Private Structure RibbonConfig
        Dim Tabs As RibbonTabs
    End Structure
    Private Ribbon As RibbonConfig

    Public Sub ResetRibbon()
        For rTab As Integer = 1 To Ribbon.Tabs.Count
            For rGroup As Integer = 1 To Ribbon.Tabs.Details(rTab)._Groups.Count
                For rButton As Integer = 1 To Ribbon.Tabs.Details(rTab)._Groups.Details(rGroup)._Buttons.Count
                    For rMenu As Integer = 1 To Ribbon.Tabs.Details(rTab)._Groups.Details(rGroup)._Buttons.Details(rButton)._Menus.Count
                        Ribbon.Tabs.Details(rTab)._Groups.Details(rGroup)._Buttons.Details(rButton)._Menus.Count = 0
                        ReDim Ribbon.Tabs.Details(rTab)._Groups.Details(rGroup)._Buttons.Details(rButton)._Menus.Details(0)
                    Next
                    With Ribbon.Tabs.Details(rTab)._Groups.Details(rGroup)._Buttons.Details(rButton)
                        RemoveHandler .Fader.FadeTimer.Tick, AddressOf TabGroupButtonFade
                        .Fader.FadeTimer.Dispose()

                        With .ObjL
                            RemoveHandler .Click, AddressOf TabGroupButton_Click
                            RemoveHandler .MouseEnter, AddressOf TabGroupButton_MouseEnter
                            RemoveHandler .MouseLeave, AddressOf TabGroupButton_MouseLeave
                            RemoveHandler .MouseDown, AddressOf TabGroupButton_MouseDown
                            RemoveHandler .MouseUp, AddressOf TabGroupButton_MouseUp
                            .Dispose()
                        End With

                        With .ObjC
                            RemoveHandler .Click, AddressOf TabGroupButton_Click
                            RemoveHandler .MouseEnter, AddressOf TabGroupButton_MouseEnter
                            RemoveHandler .MouseLeave, AddressOf TabGroupButton_MouseLeave
                            RemoveHandler .MouseDown, AddressOf TabGroupButton_MouseDown
                            RemoveHandler .MouseUp, AddressOf TabGroupButton_MouseUp
                            .Dispose()
                        End With

                        With .ObjR
                            RemoveHandler .Click, AddressOf TabGroupButton_Click
                            RemoveHandler .MouseEnter, AddressOf TabGroupButton_MouseEnter
                            RemoveHandler .MouseLeave, AddressOf TabGroupButton_MouseLeave
                            RemoveHandler .MouseDown, AddressOf TabGroupButton_MouseDown
                            RemoveHandler .MouseUp, AddressOf TabGroupButton_MouseUp
                            .Dispose()
                        End With

                        With .ObjPicture
                            RemoveHandler .Click, AddressOf TabGroupButton_Click
                            RemoveHandler .MouseEnter, AddressOf TabGroupButton_MouseEnter
                            RemoveHandler .MouseLeave, AddressOf TabGroupButton_MouseLeave
                            RemoveHandler .MouseDown, AddressOf TabGroupButton_MouseDown
                            RemoveHandler .MouseUp, AddressOf TabGroupButton_MouseUp
                            .Dispose()
                        End With

                        With .ObjText
                            RemoveHandler .Click, AddressOf TabGroupButton_Click
                            RemoveHandler .MouseEnter, AddressOf TabGroupButton_MouseEnter
                            RemoveHandler .MouseLeave, AddressOf TabGroupButton_MouseLeave
                            RemoveHandler .MouseDown, AddressOf TabGroupButton_MouseDown
                            RemoveHandler .MouseUp, AddressOf TabGroupButton_MouseUp
                            .Dispose()
                        End With

                        With .ObjMore
                            RemoveHandler .Click, AddressOf TabGroupButton_Click
                            RemoveHandler .MouseEnter, AddressOf TabGroupButton_MouseEnter
                            RemoveHandler .MouseLeave, AddressOf TabGroupButton_MouseLeave
                            RemoveHandler .MouseDown, AddressOf TabGroupButton_MouseDown
                            RemoveHandler .MouseUp, AddressOf TabGroupButton_MouseUp
                            .Dispose()
                        End With
                    End With
                Next
                With Ribbon.Tabs.Details(rTab)._Groups.Details(rGroup)
                    RemoveHandler .Fader.FadeTimer.Tick, AddressOf TabGroupButtonFade
                    .Fader.FadeTimer.Dispose()

                    With .ObjL
                        RemoveHandler .Click, AddressOf TabGroupButton_Click
                        RemoveHandler .MouseEnter, AddressOf TabGroupButton_MouseEnter
                        RemoveHandler .MouseLeave, AddressOf TabGroupButton_MouseLeave
                        RemoveHandler .MouseDown, AddressOf TabGroupButton_MouseDown
                        RemoveHandler .MouseUp, AddressOf TabGroupButton_MouseUp
                        .Dispose()
                    End With

                    With .ObjC
                        RemoveHandler .Click, AddressOf TabGroupButton_Click
                        RemoveHandler .MouseEnter, AddressOf TabGroupButton_MouseEnter
                        RemoveHandler .MouseLeave, AddressOf TabGroupButton_MouseLeave
                        RemoveHandler .MouseDown, AddressOf TabGroupButton_MouseDown
                        RemoveHandler .MouseUp, AddressOf TabGroupButton_MouseUp
                        .Dispose()
                    End With

                    With .ObjR
                        RemoveHandler .Click, AddressOf TabGroupButton_Click
                        RemoveHandler .MouseEnter, AddressOf TabGroupButton_MouseEnter
                        RemoveHandler .MouseLeave, AddressOf TabGroupButton_MouseLeave
                        RemoveHandler .MouseDown, AddressOf TabGroupButton_MouseDown
                        RemoveHandler .MouseUp, AddressOf TabGroupButton_MouseUp
                        .Dispose()
                    End With

                    With .ObjText
                        RemoveHandler .Click, AddressOf TabGroupButton_Click
                        RemoveHandler .MouseEnter, AddressOf TabGroupButton_MouseEnter
                        RemoveHandler .MouseLeave, AddressOf TabGroupButton_MouseLeave
                        RemoveHandler .MouseDown, AddressOf TabGroupButton_MouseDown
                        RemoveHandler .MouseUp, AddressOf TabGroupButton_MouseUp
                        .Dispose()
                    End With

                    With .objPopOut
                        RemoveHandler .Click, AddressOf TabGroupButton_Click
                        RemoveHandler .MouseEnter, AddressOf TabGroupButton_MouseEnter
                        RemoveHandler .MouseLeave, AddressOf TabGroupButton_MouseLeave
                        RemoveHandler .MouseDown, AddressOf TabGroupButton_MouseDown
                        RemoveHandler .MouseUp, AddressOf TabGroupButton_MouseUp
                        .Dispose()
                    End With
                End With
            Next
            With Ribbon.Tabs.Details(rTab)
                RemoveHandler .Fader.FadeTimer.Tick, AddressOf TabGroupButtonFade
                .Fader.FadeTimer.Dispose()

                With .ObjL
                    RemoveHandler .Click, AddressOf TabGroupButton_Click
                    RemoveHandler .MouseEnter, AddressOf TabGroupButton_MouseEnter
                    RemoveHandler .MouseLeave, AddressOf TabGroupButton_MouseLeave
                    RemoveHandler .MouseDown, AddressOf TabGroupButton_MouseDown
                    RemoveHandler .MouseUp, AddressOf TabGroupButton_MouseUp
                    .Dispose()
                End With

                With .ObjC
                    RemoveHandler .Click, AddressOf TabGroupButton_Click
                    RemoveHandler .MouseEnter, AddressOf TabGroupButton_MouseEnter
                    RemoveHandler .MouseLeave, AddressOf TabGroupButton_MouseLeave
                    RemoveHandler .MouseDown, AddressOf TabGroupButton_MouseDown
                    RemoveHandler .MouseUp, AddressOf TabGroupButton_MouseUp
                    .Dispose()
                End With

                With .ObjR
                    RemoveHandler .Click, AddressOf TabGroupButton_Click
                    RemoveHandler .MouseEnter, AddressOf TabGroupButton_MouseEnter
                    RemoveHandler .MouseLeave, AddressOf TabGroupButton_MouseLeave
                    RemoveHandler .MouseDown, AddressOf TabGroupButton_MouseDown
                    RemoveHandler .MouseUp, AddressOf TabGroupButton_MouseUp
                    .Dispose()
                End With

                With .ObjText
                    RemoveHandler .Click, AddressOf TabGroupButton_Click
                    RemoveHandler .MouseEnter, AddressOf TabGroupButton_MouseEnter
                    RemoveHandler .MouseLeave, AddressOf TabGroupButton_MouseLeave
                    RemoveHandler .MouseDown, AddressOf TabGroupButton_MouseDown
                    RemoveHandler .MouseUp, AddressOf TabGroupButton_MouseUp
                    .Dispose()
                End With
            End With
        Next
        Ribbon.Tabs.Count = 0
        ReDim Ribbon.Tabs.Details(0)
    End Sub

    Public Sub TabAdd(ByVal zText As String, ByVal zEnabled As Boolean, ByVal zToolTip As String)
        Dim lTab As Integer
        Dim lGraphics As Graphics = Me.CreateGraphics

        Dim iLoop As Integer
        For iLoop = 1 To Ribbon.Tabs.Count
            If Ribbon.Tabs.Details(iLoop).Text = zText Then
                If RibbonSupressErrors = False Then
                    MsgBox("Tab '" & zText & "' already specified", MsgBoxStyle.Exclamation, "Ribbon Error")
                End If
                Exit Sub
            End If
        Next

        lTab = Ribbon.Tabs.Count + 1
        Ribbon.Tabs.Count = lTab
        ReDim Preserve Ribbon.Tabs.Details(lTab)

        If lTab = 1 Then mCurrentTab = zText

        With Ribbon.Tabs.Details(lTab)
            .ID = lTab
            .Text = zText
            .Enabled = zEnabled
            .ToolTip = zToolTip
            .ObjL = New PictureBox
            .ObjC = New PictureBox
            .ObjR = New PictureBox
            .ObjText = New Label

            With .Fader
                .FadeIn = True
                .FadeSpeed = 0.05
                .FadeTimer = New Timer
                .FadeAlpha = 0.0
                AddHandler .FadeTimer.Tick, AddressOf TabFade
                .FadeTimer.Tag = Ribbon.Tabs.Details(lTab).ID
                .FadeTimer.Enabled = False
                .FadeTimer.Interval = 1
            End With

            'Setup the Button objL
            .ObjL.Name = "TabL_" & .ID
            .ObjL.Tag = .ID
            If lTab = 1 Then
                .ObjL.Left = 5
            Else
                .ObjL.Left = Ribbon.Tabs.Details(lTab - 1).ObjR.Left + Ribbon.Tabs.Details(lTab - 1).ObjR.Width
            End If
            .ObjL.Top = 2
            .ObjL.Width = 10
            .ObjL.Height = 24
            .ObjL.BackColor = Color.Transparent
            If lTab = 1 Then .ObjL.BackgroundImage = TabNormalLeft.Image
            AddHandler .ObjL.Click, AddressOf Tab_Click
            AddHandler .ObjL.MouseEnter, AddressOf Tab_MouseEnter
            AddHandler .ObjL.MouseLeave, AddressOf Tab_MouseLeave
            Me.Controls.Add(.ObjL)
            .ObjL.BringToFront()
            .ObjL.Visible = True

            'Setup the Button objC
            .ObjC.Name = "TabC_" & .ID
            .ObjC.Tag = .ID
            .ObjC.Left = .ObjL.Left + .ObjL.Width
            .ObjC.Top = 2
            .ObjC.Width = lGraphics.MeasureString(zText, TabText.Font).Width + 7
            .ObjC.Height = 24
            .ObjC.BackgroundImageLayout = ImageLayout.Stretch
            .ObjC.BackColor = Color.Transparent
            If lTab = 1 Then .ObjC.BackgroundImage = TabNormalCenter.BackgroundImage
            AddHandler .ObjC.Click, AddressOf Tab_Click
            AddHandler .ObjC.MouseEnter, AddressOf Tab_MouseEnter
            AddHandler .ObjC.MouseLeave, AddressOf Tab_MouseLeave
            Me.Controls.Add(.ObjC)
            .ObjC.BringToFront()
            .ObjC.Visible = True

            'Setup the Button objR
            .ObjR.Name = "TabR_" & .ID
            .ObjR.Tag = .ID
            .ObjR.Left = .ObjC.Left + .ObjC.Width
            .ObjR.Top = 2
            .ObjR.Width = 10
            .ObjR.Height = 24
            .ObjR.BackColor = Color.Transparent
            If lTab = 1 Then .ObjR.BackgroundImage = TabNormalRight.Image
            AddHandler .ObjR.Click, AddressOf Tab_Click
            AddHandler .ObjR.MouseEnter, AddressOf Tab_MouseEnter
            AddHandler .ObjR.MouseLeave, AddressOf Tab_MouseLeave
            Me.Controls.Add(.ObjR)
            .ObjR.BringToFront()
            .ObjR.Visible = True

            'Setup the Button objText
            .ObjText.Parent = .ObjC
            .ObjText.Name = "TabText_" & .ID
            .ObjText.Tag = .ID
            .ObjText.Left = 0
            .ObjText.Top = 0
            .ObjText.Width = .ObjC.Width
            .ObjText.Height = 24
            .ObjText.BackColor = Color.Transparent
            .ObjText.Font = TabText.Font
            .ObjText.ForeColor = TabText.ForeColor
            .ObjText.Text = zText
            .ObjText.TextAlign = ContentAlignment.MiddleCenter
            mToolTips.SetToolTip(.ObjText, .ToolTip)
            AddHandler .ObjText.Click, AddressOf Tab_Click
            AddHandler .ObjText.MouseEnter, AddressOf Tab_MouseEnter
            AddHandler .ObjText.MouseLeave, AddressOf Tab_MouseLeave
            .ObjC.Controls.Add(.ObjText)
            .ObjText.BringToFront()
            .ObjText.Visible = True
        End With
    End Sub
    Public Property TabCurrent() As String
        Get
            Return mCurrentTab
        End Get
        Set(ByVal value As String)
            For rLoop As Integer = 1 To Ribbon.Tabs.Count
                If Ribbon.Tabs.Details(rLoop).Text = value Then
                    TabSet(rLoop)
                    Exit For
                End If
            Next
        End Set
    End Property

    Private Sub Tab_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        TabSet(Val(sender.Tag))
    End Sub

    Private Sub TabSet(ByVal zTabID As Integer)
        Dim lTabID As Integer = zTabID
        Dim lTabText As String = Ribbon.Tabs.Details(lTabID).Text

        If lTabText = mCurrentTab Then Exit Sub

        Dim iLoopTab As Integer
        Dim iLoopGroup As Integer
        Dim iLoopButton As Integer
        For iLoopTab = 1 To Ribbon.Tabs.Count
            Ribbon.Tabs.Details(iLoopTab).ObjL.BackgroundImage = Nothing
            Ribbon.Tabs.Details(iLoopTab).ObjC.BackgroundImage = Nothing
            Ribbon.Tabs.Details(iLoopTab).ObjR.BackgroundImage = Nothing
            For iLoopGroup = 1 To Ribbon.Tabs.Details(iLoopTab)._Groups.Count
                With Ribbon.Tabs.Details(iLoopTab)._Groups.Details(iLoopGroup)
                    .ObjL.Visible = False
                    .ObjC.Visible = False
                    .ObjR.Visible = False
                    .ObjText.Visible = False
                    .objPopOut.Visible = .PopOut
                    For iLoopButton = 1 To ._Buttons.Count
                        With ._Buttons.Details(iLoopButton)
                            .ObjL.Visible = False
                            .ObjC.Visible = False
                            .ObjR.Visible = False
                            .ObjText.Visible = False
                        End With
                    Next
                End With
            Next
        Next
        Ribbon.Tabs.Details(lTabID).ObjL.BackgroundImage = TabNormalLeft.Image
        Ribbon.Tabs.Details(lTabID).ObjC.BackgroundImage = TabNormalCenter.BackgroundImage
        Ribbon.Tabs.Details(lTabID).ObjR.BackgroundImage = TabNormalRight.Image
        For iLoopGroup = 1 To Ribbon.Tabs.Details(lTabID)._Groups.Count
            With Ribbon.Tabs.Details(lTabID)._Groups.Details(iLoopGroup)
                .ObjL.Visible = True
                .ObjC.Visible = True
                .ObjR.Visible = True
                .ObjText.Visible = True

                For iLoopButton = 1 To ._Buttons.Count
                    With ._Buttons.Details(iLoopButton)
                        .ObjL.Visible = True
                        .ObjC.Visible = True
                        .ObjR.Visible = True
                        .ObjText.Visible = True
                    End With
                Next
            End With
        Next

        mCurrentTab = lTabText
        RaiseEvent ClickedTab(lTabID, lTabText)
    End Sub
    Private Sub Tab_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lTabID As Integer = Val(sender.Tag)
        Dim lTabText As String = Ribbon.Tabs.Details(lTabID).Text

        If lTabText = mCurrentTab Then Exit Sub

        Dim lCtlName As String = Mid(sender.Name, 1, InStr(sender.Name, "_"))
        Me.Controls(Replace(sender.Name, lCtlName, "TabL_")).BackgroundImage = TabOverLeft.Image
        Me.Controls(Replace(sender.Name, lCtlName, "TabC_")).BackgroundImage = TabOverCenter.BackgroundImage
        Me.Controls(Replace(sender.Name, lCtlName, "TabR_")).BackgroundImage = TabOverRight.Image
    End Sub
    Private Sub Tab_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lTabID As Integer = Val(sender.Tag)
        Dim lTabText As String = Ribbon.Tabs.Details(lTabID).Text

        If lTabText = mCurrentTab Then Exit Sub

        Dim lCtlName As String = Mid(sender.Name, 1, InStr(sender.Name, "_"))
        Me.Controls(Replace(sender.Name, lCtlName, "TabL_")).BackgroundImage = Nothing
        Me.Controls(Replace(sender.Name, lCtlName, "TabC_")).BackgroundImage = Nothing
        Me.Controls(Replace(sender.Name, lCtlName, "TabR_")).BackgroundImage = Nothing
    End Sub
    Private Sub TabFade(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim lTemp() As String = sender.Tag.ToString.Split("_")
        Dim lTabID As Integer = Val(lTemp(0))

        FadeAdjust(Ribbon.Tabs.Details(lTabID).Fader)
        FadeImage(Ribbon.Tabs.Details(lTabID).ObjL.Image, TabNormalLeft.Image, TabOverLeft.Image, 0, Ribbon.Tabs.Details(lTabID).Fader.FadeAlpha)
        FadeImage(Ribbon.Tabs.Details(lTabID).ObjC.BackgroundImage, TabNormalCenter.BackgroundImage, TabOverCenter.BackgroundImage, 0, Ribbon.Tabs.Details(lTabID).Fader.FadeAlpha)
        FadeImage(Ribbon.Tabs.Details(lTabID).ObjR.Image, TabNormalRight.Image, TabOverRight.Image, 0, Ribbon.Tabs.Details(lTabID).Fader.FadeAlpha)
    End Sub

    Public Sub TabGroupAdd(ByVal zTab As String, ByVal zText As String, ByVal zPopout As Boolean, ByVal zToolTip As String)
        Dim lTab As Integer
        Dim lGroup As Integer
        Dim lGraphics As Graphics = Me.CreateGraphics

        Dim iLoop As Integer
        For iLoop = 1 To Ribbon.Tabs.Count
            If Ribbon.Tabs.Details(iLoop).Text = zTab Then
                lTab = iLoop
                Exit For
            End If
        Next
        If lTab = 0 Then
            If RibbonSupressErrors = False Then
                MsgBox("Tab '" & zTab & "' not found", MsgBoxStyle.Exclamation, "Ribbon Error")
            End If
            Exit Sub
        End If

        For iLoop = 1 To Ribbon.Tabs.Details(lTab)._Groups.Count
            If Ribbon.Tabs.Details(lTab)._Groups.Details(iLoop).Text = zText Then
                If RibbonSupressErrors = False Then
                    MsgBox("TabGroup '" & zText & "' already specified", MsgBoxStyle.Exclamation, "Ribbon Error")
                End If
                Exit Sub
            End If
        Next

        lGroup = Ribbon.Tabs.Details(lTab)._Groups.Count + 1
        Ribbon.Tabs.Details(lTab)._Groups.Count = lGroup
        ReDim Preserve Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup)

        With Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup)
            .ID = lTab & "_" & lGroup
            .Text = zText
            .PopOut = zPopout
            .PopOutOver = False
            .ToolTip = zToolTip
            .ObjL = New PictureBox
            .ObjC = New PictureBox
            .ObjR = New PictureBox
            .ObjText = New Label
            .objPopOut = New PictureBox

            With .Fader
                .FadeIn = True
                .FadeSpeed = 0.05
                .FadeTimer = New Timer
                .FadeAlpha = 0.0
                AddHandler .FadeTimer.Tick, AddressOf TabGroupFade
                .FadeTimer.Tag = Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup).ID
                .FadeTimer.Enabled = False
                .FadeTimer.Interval = 1
            End With

            'Setup the Button objL
            .ObjL.Name = "TabGroupL_" & .ID
            .ObjL.Tag = .ID
            If lGroup = 1 Then
                .ObjL.Left = 3
            Else
                .ObjL.Left = Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup - 1).ObjR.Left + Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup - 1).ObjR.Width
            End If
            .ObjL.Top = 26
            .ObjL.Width = 4
            .ObjL.Height = 89
            .ObjL.BackColor = Color.Transparent
            .ObjL.BackgroundImage = GroupNormalLeft.Image
            AddHandler .ObjL.Click, AddressOf TabGroup_Click
            AddHandler .ObjL.MouseEnter, AddressOf TabGroup_MouseEnter
            AddHandler .ObjL.MouseLeave, AddressOf TabGroup_MouseLeave
            Me.Controls.Add(.ObjL)
            .ObjL.BringToFront()
            .ObjL.Visible = IIf(lTab = 1, True, False)

            'Setup the Button objC
            .ObjC.Name = "TabGroupC_" & .ID
            .ObjC.Tag = .ID
            .ObjC.Left = .ObjL.Left + .ObjL.Width
            .ObjC.Top = .ObjL.Top
            .ObjC.Width = lGraphics.MeasureString("  " & zText & "  ", GroupText.Font).ToSize.Width + IIf(.PopOut = True, 15, 0)
            .ObjC.Height = 89
            .ObjC.BackgroundImageLayout = ImageLayout.Tile
            .ObjC.BackColor = Color.Transparent
            .ObjC.BackgroundImage = GroupNormalCenter.BackgroundImage
            AddHandler .ObjC.Click, AddressOf TabGroup_Click
            AddHandler .ObjC.MouseEnter, AddressOf TabGroup_MouseEnter
            AddHandler .ObjC.MouseLeave, AddressOf TabGroup_MouseLeave
            Me.Controls.Add(.ObjC)
            .ObjC.BringToFront()
            .ObjC.Visible = IIf(lTab = 1, True, False)

            'Setup the Button objR
            .ObjR.Name = "TabGroupR_" & .ID
            .ObjR.Tag = .ID
            .ObjR.Left = .ObjC.Left + .ObjC.Width
            .ObjR.Top = .ObjL.Top
            .ObjR.Width = 4
            .ObjR.Height = 89
            .ObjR.BackColor = Color.Transparent
            .ObjR.BackgroundImage = GroupNormalRight.Image
            AddHandler .ObjR.Click, AddressOf TabGroup_Click
            AddHandler .ObjR.MouseEnter, AddressOf TabGroup_MouseEnter
            AddHandler .ObjR.MouseLeave, AddressOf TabGroup_MouseLeave
            Me.Controls.Add(.ObjR)
            .ObjR.BringToFront()
            .ObjR.Visible = IIf(lTab = 1, True, False)

            'Setup the Button objText
            .ObjText.Parent = .ObjC
            .ObjText.Name = "TabGroupText_" & .ID
            .ObjText.Tag = .ID
            .ObjText.Left = 0
            .ObjText.Top = 89 - GroupText.Height - 3
            .ObjText.Width = .ObjC.Width - IIf(.PopOut = True, 17, 0)
            .ObjText.Height = GroupText.Height
            .ObjText.BackColor = Color.Transparent
            .ObjText.Font = GroupText.Font
            .ObjText.ForeColor = GroupText.ForeColor
            .ObjText.Text = zText
            .ObjText.TextAlign = ContentAlignment.MiddleCenter
            mToolTips.SetToolTip(.ObjText, .ToolTip)
            AddHandler .ObjText.Click, AddressOf TabGroup_Click
            AddHandler .ObjText.MouseEnter, AddressOf TabGroup_MouseEnter
            AddHandler .ObjText.MouseLeave, AddressOf TabGroup_MouseLeave
            .ObjC.Controls.Add(.ObjText)
            .ObjText.BringToFront()
            .ObjText.Visible = IIf(lTab = 1, True, False)

            'Setup the Button objPopOut
            .objPopOut.Name = "TabGroupPopOut_" & .ID
            .objPopOut.Tag = .ID
            .objPopOut.Left = .ObjC.Width - 14
            .objPopOut.Top = .ObjC.Height - 15 - 3
            .objPopOut.Width = 15
            .objPopOut.Height = 14
            .objPopOut.BackColor = Color.Transparent
            If .PopOut = True Then .objPopOut.Image = PopoutGroupNormal.Image
            AddHandler .objPopOut.Click, AddressOf TabGroup_Click
            AddHandler .objPopOut.MouseEnter, AddressOf TabGroup_MouseEnter
            AddHandler .objPopOut.MouseLeave, AddressOf TabGroup_MouseLeave
            .ObjC.Controls.Add(.objPopOut)
            .objPopOut.BringToFront()
            .objPopOut.Visible = False
            If .PopOut = True Then .objPopOut.Visible = IIf(lTab = 1, True, False)
        End With
    End Sub
    Private Sub TabGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim lTemp() As String = sender.Tag.ToString.Split("_")
        Dim lTabID As Integer = Val(lTemp(0))
        Dim lTabText As String = Ribbon.Tabs.Details(lTabID).Text
        Dim lGroupID As Integer = Val(lTemp(1))
        Dim lGroupText As String = Ribbon.Tabs.Details(lTabID)._Groups.Details(lGroupID).Text

        If Mid(sender.Name, 1, 15) = "TabGroupPopOut_" Then
            RaiseEvent ClickedTabGroupPopout(sender.Tag, lTabText, lGroupText)
        Else
            RaiseEvent ClickedTabGroup(sender.Tag, lTabText, lGroupText)
        End If
    End Sub
    Private Sub TabGroup_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lTemp() As String = sender.Tag.ToString.Split("_")
        Dim lTabID As Integer = Val(lTemp(0))
        Dim lGroupID As Integer = Val(lTemp(1))

        With Ribbon.Tabs.Details(lTabID)._Groups.Details(lGroupID)
            With .Fader
                .FadeIn = True
                .FadeTimer.Enabled = True
            End With

            If Mid(sender.Name, 1, 15) = "TabGroupPopOut_" Then
                .PopOutOver = True
            End If
        End With
    End Sub
    Private Sub TabGroup_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lTemp() As String = sender.Tag.ToString.Split("_")
        Dim lTabID As Integer = Val(lTemp(0))
        Dim lGroupID As Integer = Val(lTemp(1))

        With Ribbon.Tabs.Details(lTabID)._Groups.Details(lGroupID)
            With .Fader
                .FadeIn = False
                .FadeTimer.Enabled = True
            End With
            If Mid(sender.Name, 1, 15) = "TabGroupPopOut_" Then
                .PopOutOver = False
            End If
        End With
    End Sub
    Private Sub TabGroupFade(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim lTemp() As String = sender.Tag.ToString.Split("_")
        Dim lTabID As Integer = Val(lTemp(0))
        Dim lGroupID As Integer = Val(lTemp(1))

        With Ribbon.Tabs.Details(lTabID)._Groups.Details(lGroupID)
            FadeAdjust(.Fader)
            FadeImage(.ObjL.Image, GroupNormalLeft.Image, GroupOverLeft.Image, 0, .Fader.FadeAlpha)
            FadeImage(.ObjC.BackgroundImage, GroupNormalCenter.BackgroundImage, GroupOverCenter.BackgroundImage, 2, .Fader.FadeAlpha)
            FadeImage(.ObjR.Image, GroupNormalRight.Image, GroupOverRight.Image, 0, .Fader.FadeAlpha)

            If .PopOut = True Then
                If .PopOutOver = True Then
                    .objPopOut.Image = PopoutOver.Image
                Else
                    FadeImage(.objPopOut.Image, PopoutGroupNormal.Image, PopoutGroupOver.Image, 0, .Fader.FadeAlpha)
                End If
            End If
        End With
    End Sub

    Public Sub TabGroupButtonAdd(ByVal zTab As String, ByVal zGroup As String, ByVal zText As String, ByVal zEnabled As Boolean, ByVal zPicture As Image, ByVal zMore As Boolean, ByVal zToolTip As String)
        Dim lTab As Integer
        Dim lGroup As Integer
        Dim lButton As Integer
        Dim lTemp As Integer
        Dim lGraphics As Graphics = Me.CreateGraphics

        Dim iLoop As Integer
        For iLoop = 1 To Ribbon.Tabs.Count
            If Ribbon.Tabs.Details(iLoop).Text = zTab Then
                lTab = iLoop
                Exit For
            End If
        Next
        If lTab = 0 Then
            If RibbonSupressErrors = False Then
                MsgBox("Tab '" & zTab & "' not found", MsgBoxStyle.Exclamation, "Ribbon Error")
            End If
            Exit Sub
        End If

        For iLoop = 1 To Ribbon.Tabs.Details(lTab)._Groups.Count
            If Ribbon.Tabs.Details(lTab)._Groups.Details(iLoop).Text = zGroup Then
                lGroup = iLoop
                Exit For
            End If
        Next
        If lGroup = 0 Then
            If RibbonSupressErrors = False Then
                MsgBox("TabGroup '" & zGroup & "' not found", MsgBoxStyle.Exclamation, "Ribbon Error")
            End If
            Exit Sub
        End If

        For iLoop = 1 To Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup)._Buttons.Count
            If Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup)._Buttons.Details(iLoop).Text = zText Then
                If RibbonSupressErrors = False Then
                    MsgBox("TabGroupButton '" & zText & "' already specified", MsgBoxStyle.Exclamation, "Ribbon Error")
                End If
                Exit Sub
            End If
        Next

        lButton = Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup)._Buttons.Count + 1
        Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup)._Buttons.Count = lButton
        ReDim Preserve Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup)._Buttons.Details(lButton)

        With Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup)._Buttons.Details(lButton)
            .ID = lTab & "_" & lGroup & "_" & lButton
            .Text = zText
            .Enabled = zEnabled
            .Picture = zPicture
            .More = zMore
            .ToolTip = zToolTip
            .ObjL = New PictureBox
            .ObjC = New PictureBox
            .ObjR = New PictureBox
            .ObjPicture = New PictureBox
            .ObjText = New Label
            .ObjMore = New PictureBox

            With .Fader
                .FadeIn = True
                .FadeSpeed = 0.05
                .FadeTimer = New Timer
                .FadeAlpha = 0.0
                AddHandler .FadeTimer.Tick, AddressOf TabGroupButtonFade
                .FadeTimer.Tag = Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup)._Buttons.Details(lButton).ID
                .FadeTimer.Enabled = False
                .FadeTimer.Interval = 1
            End With

            'Setup the Button objL
            .ObjL.Name = "TabGroupButtonL_" & .ID
            .ObjL.Tag = .ID
            If lButton = 1 Then
                .ObjL.Left = 0
            Else
                .ObjL.Left = Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup)._Buttons.Details(lButton - 1).ObjR.Left + Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup)._Buttons.Details(lButton - 1).ObjR.Width
            End If
            .ObjL.Top = 4
            .ObjL.Width = 3
            .ObjL.Height = 66
            .ObjL.BackColor = Color.Transparent
            AddHandler .ObjL.Click, AddressOf TabGroupButton_Click
            AddHandler .ObjL.MouseEnter, AddressOf TabGroupButton_MouseEnter
            AddHandler .ObjL.MouseLeave, AddressOf TabGroupButton_MouseLeave
            AddHandler .ObjL.MouseDown, AddressOf TabGroupButton_MouseDown
            AddHandler .ObjL.MouseUp, AddressOf TabGroupButton_MouseUp
            Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup).ObjC.Controls.Add(.ObjL)
            .ObjL.BringToFront()
            .ObjL.Visible = IIf(lTab = 1, True, False)

            'Setup the Button objC
            .ObjC.Name = "TabGroupButtonC_" & .ID
            .ObjC.Tag = .ID
            .ObjC.Left = .ObjL.Left + .ObjL.Width
            .ObjC.Top = 4
            .ObjC.Width = lGraphics.MeasureString("  " & zText & "  ", ButtonText.Font).Width + 10
            If .ObjC.Width < 34 Then .ObjC.Width = 34
            .ObjC.Height = 66
            .ObjC.BackgroundImageLayout = ImageLayout.Tile
            .ObjC.BackColor = Color.Transparent
            mToolTips.SetToolTip(.ObjC, .ToolTip)
            AddHandler .ObjC.Click, AddressOf TabGroupButton_Click
            AddHandler .ObjC.MouseEnter, AddressOf TabGroupButton_MouseEnter
            AddHandler .ObjC.MouseLeave, AddressOf TabGroupButton_MouseLeave
            AddHandler .ObjC.MouseDown, AddressOf TabGroupButton_MouseDown
            AddHandler .ObjC.MouseUp, AddressOf TabGroupButton_MouseUp
            Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup).ObjC.Controls.Add(.ObjC)
            .ObjC.BringToFront()
            .ObjC.Visible = IIf(lTab = 1, True, False)

            'Setup the Button objR
            .ObjR.Name = "TabGroupButtonR_" & .ID
            .ObjR.Tag = .ID
            .ObjR.Left = .ObjC.Left + .ObjC.Width
            .ObjR.Top = 4
            .ObjR.Width = 4
            .ObjR.Height = 66
            .ObjR.BackColor = Color.Transparent
            AddHandler .ObjR.Click, AddressOf TabGroupButton_Click
            AddHandler .ObjR.MouseEnter, AddressOf TabGroupButton_MouseEnter
            AddHandler .ObjR.MouseLeave, AddressOf TabGroupButton_MouseLeave
            AddHandler .ObjR.MouseDown, AddressOf TabGroupButton_MouseDown
            AddHandler .ObjR.MouseUp, AddressOf TabGroupButton_MouseUp
            Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup).ObjC.Controls.Add(.ObjR)
            .ObjR.BringToFront()
            .ObjR.Visible = IIf(lTab = 1, True, False)

            'Setup the Button objPicture
            .ObjPicture.Name = "TabGroupButtonPicture_" & .ID
            .ObjPicture.Tag = .ID
            .ObjPicture.Width = 32
            .ObjPicture.Height = 32
            .ObjPicture.Left = (.ObjC.Width - .ObjPicture.Width) / 2
            .ObjPicture.Top = 4
            .ObjPicture.SizeMode = PictureBoxSizeMode.Normal
            .ObjPicture.BackColor = Color.Transparent
            .ObjPicture.Image = .Picture
            mToolTips.SetToolTip(.ObjPicture, .ToolTip)
            AddHandler .ObjPicture.Click, AddressOf TabGroupButton_Click
            AddHandler .ObjPicture.MouseEnter, AddressOf TabGroupButton_MouseEnter
            AddHandler .ObjPicture.MouseLeave, AddressOf TabGroupButton_MouseLeave
            AddHandler .ObjPicture.MouseDown, AddressOf TabGroupButton_MouseDown
            AddHandler .ObjPicture.MouseUp, AddressOf TabGroupButton_MouseUp
            .ObjC.Controls.Add(.ObjPicture)
            .ObjPicture.BringToFront()
            .ObjPicture.Visible = True

            'Setup the Button objText
            .ObjText.Parent = .ObjC
            .ObjText.Name = "TabGroupButtonText_" & .ID
            .ObjText.Tag = .ID
            .ObjText.Left = 0
            .ObjText.Top = 36
            .ObjText.Width = .ObjC.Width
            .ObjText.Height = 30
            .ObjText.BackColor = Color.Transparent
            .ObjText.Font = ButtonText.Font
            .ObjText.ForeColor = ButtonText.ForeColor
            .ObjText.Text = zText
            .ObjText.TextAlign = ContentAlignment.TopCenter
            mToolTips.SetToolTip(.ObjText, .ToolTip)
            AddHandler .ObjText.Click, AddressOf TabGroupButton_Click
            AddHandler .ObjText.MouseEnter, AddressOf TabGroupButton_MouseEnter
            AddHandler .ObjText.MouseLeave, AddressOf TabGroupButton_MouseLeave
            AddHandler .ObjText.MouseDown, AddressOf TabGroupButton_MouseDown
            AddHandler .ObjText.MouseUp, AddressOf TabGroupButton_MouseUp
            .ObjC.Controls.Add(.ObjText)
            .ObjText.BringToFront()
            .ObjText.Visible = True

            'Setup the Button objMore
            '.ObjMore.Parent = .ObjC
            .ObjMore.Name = "TabGroupButtonMore_" & .ID
            .ObjMore.Tag = .ID
            .ObjMore.Top = 60
            .ObjMore.Width = 5
            .ObjMore.Height = 4
            .ObjMore.Left = (.ObjC.Width - .ObjMore.Width) / 2
            .ObjMore.BackColor = Color.Transparent
            .ObjMore.Image = DropdownNormal.Image
            mToolTips.SetToolTip(.ObjMore, .ToolTip)
            AddHandler .ObjMore.Click, AddressOf TabGroupButton_Click
            AddHandler .ObjMore.MouseEnter, AddressOf TabGroupButton_MouseEnter
            AddHandler .ObjMore.MouseLeave, AddressOf TabGroupButton_MouseLeave
            AddHandler .ObjMore.MouseDown, AddressOf TabGroupButton_MouseDown
            AddHandler .ObjMore.MouseUp, AddressOf TabGroupButton_MouseUp
            .ObjC.Controls.Add(.ObjMore)
            .ObjMore.BringToFront()
            .ObjMore.Visible = .More

            If .ObjR.Left + .ObjR.Width + 6 > Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup).ObjC.Width Then
                Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup).ObjC.Width = .ObjR.Left + .ObjR.Width + 3
                Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup).ObjText.Width = Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup).ObjC.Width - IIf(Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup).PopOut = True, 20, 0)
                Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup).ObjR.Left = Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup).ObjL.Left + Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup).ObjText.Width + IIf(Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup).PopOut = True, 20, 0)
                Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup).objPopOut.Left = Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup).ObjC.Width - Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup).objPopOut.Width - 3
                For lTemp = lGroup + 1 To Ribbon.Tabs.Details(lTab)._Groups.Count
                    Ribbon.Tabs.Details(lTab)._Groups.Details(lTemp).ObjL.Left = Ribbon.Tabs.Details(lTab)._Groups.Details(lTemp - 1).ObjR.Left + Ribbon.Tabs.Details(lTab)._Groups.Details(lTemp - 1).ObjR.Width
                    Ribbon.Tabs.Details(lTab)._Groups.Details(lTemp).ObjC.Left = Ribbon.Tabs.Details(lTab)._Groups.Details(lTemp).ObjL.Left + Ribbon.Tabs.Details(lTab)._Groups.Details(lTemp).ObjL.Width
                    Ribbon.Tabs.Details(lTab)._Groups.Details(lTemp).ObjR.Left = Ribbon.Tabs.Details(lTab)._Groups.Details(lTemp).ObjC.Left + Ribbon.Tabs.Details(lTab)._Groups.Details(lTemp).ObjC.Width
                    Ribbon.Tabs.Details(lTab)._Groups.Details(lTemp).objPopOut.Left = Ribbon.Tabs.Details(lTab)._Groups.Details(lTemp).ObjC.Width - Ribbon.Tabs.Details(lTab)._Groups.Details(lTemp).objPopOut.Width - 3
                Next
            End If
        End With
    End Sub
    Private Sub TabGroupButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim lTemp() As String = sender.Tag.ToString.Split("_")
        Dim lTabID As Integer = Val(lTemp(0))
        Dim lTabText As String = Ribbon.Tabs.Details(lTabID).Text
        Dim lGroupID As Integer = Val(lTemp(1))
        Dim lGroupText As String = Ribbon.Tabs.Details(lTabID)._Groups.Details(lGroupID).Text
        Dim lButtonID As Integer = Val(lTemp(2))
        Dim lButtonText As String = Ribbon.Tabs.Details(lTabID)._Groups.Details(lGroupID)._Buttons.Details(lButtonID).Text
        Dim lLoop As Integer
        Dim lPoint As New Point

        If Ribbon.Tabs.Details(lTabID)._Groups.Details(lGroupID)._Buttons.Details(lButtonID).More = False Then
            RaiseEvent ClickedTabGroupButton(sender.Tag, lTabText, lGroupText, lButtonText)
        Else
            cmsPopup.Items.Clear()
            For lLoop = 1 To Ribbon.Tabs.Details(lTabID)._Groups.Details(lGroupID)._Buttons.Details(lButtonID)._Menus.Count
                cmsPopup.Items.Add(Ribbon.Tabs.Details(lTabID)._Groups.Details(lGroupID)._Buttons.Details(lButtonID)._Menus.Details(lLoop).Text, Ribbon.Tabs.Details(lTabID)._Groups.Details(lGroupID)._Buttons.Details(lButtonID)._Menus.Details(lLoop).Picture).Tag = Ribbon.Tabs.Details(lTabID)._Groups.Details(lGroupID)._Buttons.Details(lButtonID)._Menus.Details(lLoop).ID
            Next
            If cmsPopup.Items.Count > 0 Then
                lPoint.X = 0
                lPoint.Y = Ribbon.Tabs.Details(lTabID)._Groups.Details(lGroupID)._Buttons.Details(lButtonID).ObjC.Height
                cmsPopup.Show(Ribbon.Tabs.Details(lTabID)._Groups.Details(lGroupID)._Buttons.Details(lButtonID).ObjC, lPoint)
            End If
        End If
    End Sub
    Private Sub TabGroupButton_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lTemp() As String = sender.Tag.ToString.Split("_")
        Dim lTabID As Integer = Val(lTemp(0))
        Dim lGroupID As Integer = Val(lTemp(1))
        Dim lButtonID As Integer = Val(lTemp(2))

        With Ribbon.Tabs.Details(lTabID)._Groups.Details(lGroupID)
            .Fader.FadeIn = True
            .Fader.FadeTimer.Enabled = True

            With ._Buttons.Details(lButtonID)
                .ObjL.Image = ButtonOverLeft.Image
                .ObjC.BackgroundImage = ButtonOverCenter.BackgroundImage
                .ObjR.Image = ButtonOverRight.Image
                If Mid(sender.Name, 1, 19) = "TabGroupButtonMore_" Then
                    .ObjMore.Image = DropdownOver.Image
                End If
            End With
        End With
    End Sub
    Private Sub TabGroupButton_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lTemp() As String = sender.Tag.ToString.Split("_")
        Dim lTabID As Integer = Val(lTemp(0))
        Dim lGroupID As Integer = Val(lTemp(1))
        Dim lButtonID As Integer = Val(lTemp(2))

        With Ribbon.Tabs.Details(lTabID)._Groups.Details(lGroupID)
            .Fader.FadeIn = False
            .Fader.FadeTimer.Enabled = True

            With ._Buttons.Details(lButtonID)
                .ObjL.Image = Nothing
                .ObjC.BackgroundImage = Nothing
                .ObjR.Image = Nothing

                If Mid(sender.Name, 1, 19) = "TabGroupButtonMore_" Then
                    .ObjMore.Image = DropdownNormal.Image
                End If
            End With
        End With
    End Sub
    Private Sub TabGroupButton_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim lTemp() As String = sender.Tag.ToString.Split("_")
        Dim lTabID As Integer = Val(lTemp(0))
        Dim lGroupID As Integer = Val(lTemp(1))
        Dim lButtonID As Integer = Val(lTemp(2))

        With Ribbon.Tabs.Details(lTabID)._Groups.Details(lGroupID)._Buttons.Details(lButtonID)
            .ObjL.Image = ButtonDownLeft.Image
            .ObjC.BackgroundImage = ButtonDownCenter.BackgroundImage
            .ObjR.Image = ButtonDownRight.Image
        End With
    End Sub
    Private Sub TabGroupButton_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim lTemp() As String = sender.Tag.ToString.Split("_")
        Dim lTabID As Integer = Val(lTemp(0))
        Dim lGroupID As Integer = Val(lTemp(1))
        Dim lButtonID As Integer = Val(lTemp(2))

        With Ribbon.Tabs.Details(lTabID)._Groups.Details(lGroupID)._Buttons.Details(lButtonID)
            .ObjL.Image = ButtonOverLeft.Image
            .ObjC.BackgroundImage = ButtonOverCenter.BackgroundImage
            .ObjR.Image = ButtonOverRight.Image
        End With
    End Sub
    Private Sub TabGroupButtonFade(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim lTemp() As String = sender.Tag.ToString.Split("_")
        Dim lTabID As Integer = Val(lTemp(0))
        Dim lGroupID As Integer = Val(lTemp(1))
        Dim lButtonID As Integer = Val(lTemp(2))

        'FadeImage(.ObjL.Image, buttoNormalLeft.Image, GroupOverLeft.Image, .Fader.FadeAlpha, False)
        'FadeImage(.ObjC.BackgroundImage, GroupNormalCenter.BackgroundImage, GroupOverCenter.BackgroundImage, .Fader.FadeAlpha, True)
        'FadeImage(.ObjR.Image, GroupNormalRight.Image, GroupOverRight.Image, .Fader.FadeAlpha, False)
    End Sub

    Public Sub TabGroupButtonMenuAdd(ByVal zTab As String, ByVal zGroup As String, ByVal zButton As String, ByVal zText As String, ByVal zPicture As Image)
        Dim lTab As Integer
        Dim lGroup As Integer
        Dim lButton As Integer
        Dim lMenu As Integer

        Dim iLoop As Integer
        For iLoop = 1 To Ribbon.Tabs.Count
            If Ribbon.Tabs.Details(iLoop).Text = zTab Then
                lTab = iLoop
                Exit For
            End If
        Next
        If lTab = 0 Then
            If RibbonSupressErrors = False Then
                MsgBox("Tab '" & zTab & "' not found", MsgBoxStyle.Exclamation, "Ribbon Error")
            End If
            Exit Sub
        End If

        For iLoop = 1 To Ribbon.Tabs.Details(lTab)._Groups.Count
            If Ribbon.Tabs.Details(lTab)._Groups.Details(iLoop).Text = zGroup Then
                lGroup = iLoop
                Exit For
            End If
        Next
        If lGroup = 0 Then
            If RibbonSupressErrors = False Then
                MsgBox("TabGroup '" & zGroup & "' not found", MsgBoxStyle.Exclamation, "Ribbon Error")
            End If
            Exit Sub
        End If

        For iLoop = 1 To Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup)._Buttons.Count
            If Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup)._Buttons.Details(iLoop).Text = zButton Then
                lButton = iLoop
                Exit For
            End If
        Next
        If lButton = 0 Then
            If RibbonSupressErrors = False Then
                MsgBox("TabGroupButton '" & zButton & "' not found", MsgBoxStyle.Exclamation, "Ribbon Error")
            End If
            Exit Sub
        End If

        For iLoop = 1 To Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup)._Buttons.Details(lButton)._Menus.Count
            If Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup)._Buttons.Details(lButton)._Menus.Details(iLoop).Text = zText Then
                If RibbonSupressErrors = False Then
                    MsgBox("TabGroupButtonMenu '" & zText & "' already specified", MsgBoxStyle.Exclamation, "Ribbon Error")
                End If
                Exit Sub
            End If
        Next

        lMenu = Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup)._Buttons.Details(lButton)._Menus.Count + 1
        Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup)._Buttons.Details(lButton)._Menus.Count = lMenu
        ReDim Preserve Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup)._Buttons.Details(lButton)._Menus.Details(lMenu)
        With Ribbon.Tabs.Details(lTab)._Groups.Details(lGroup)._Buttons.Details(lButton)._Menus.Details(lMenu)
            .ID = lTab & "_" & lGroup & "_" & lButton & "_" & lMenu
            .Text = zText
            .Picture = zPicture
        End With
    End Sub

    Private Sub cmsPopup_ItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles cmsPopup.ItemClicked
        Dim lTemp() As String = e.ClickedItem.Tag.ToString.Split("_")
        Dim lTabID As Integer = Val(lTemp(0))
        Dim lTabText As String = Ribbon.Tabs.Details(lTabID).Text
        Dim lGroupID As Integer = Val(lTemp(1))
        Dim lGroupText As String = Ribbon.Tabs.Details(lTabID)._Groups.Details(lGroupID).Text
        Dim lButtonID As Integer = Val(lTemp(2))
        Dim lButtonText As String = Ribbon.Tabs.Details(lTabID)._Groups.Details(lGroupID)._Buttons.Details(lButtonID).Text
        Dim lMenuID As Integer = Val(lTemp(3))
        Dim lMenuText As String = Ribbon.Tabs.Details(lTabID)._Groups.Details(lGroupID)._Buttons.Details(lButtonID)._Menus.Details(lMenuID).Text

        RaiseEvent ClickedTabGroupButtonMenu(e.ClickedItem.Tag, lTabText, lGroupText, lButtonText, lMenuText)
    End Sub

    Private Sub uRibbon_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        Me.Height = 118
        If Me.Width <= 400 Then Me.Width = 400
    End Sub

    Private Sub FadeAdjust(ByRef zFadeItem As FadeItem)
        With zFadeItem
            If .FadeIn = True Then
                .FadeAlpha += .FadeSpeed
            Else
                .FadeAlpha -= .FadeSpeed
            End If

            If .FadeAlpha >= 1 Then
                .FadeAlpha = 1
                .FadeTimer.Enabled = False
            ElseIf .FadeAlpha <= 0 Then
                .FadeAlpha = 0
                .FadeTimer.Enabled = False
            End If
        End With
    End Sub

    Private Sub FadeImage(ByRef zFadeImage As Image, ByVal zFadeImg1 As Image, ByVal zFadeImg2 As Image, ByVal zOffset As Integer, ByRef zFadeAlpha As Single)
        Dim i1 As New Bitmap(zFadeImg1)
        Dim i2 As New Bitmap(zFadeImg2)

        Dim x As Integer = IIf(i1.Width < i2.Width, i1.Width, i2.Width)
        Dim y As Integer = IIf(i1.Height < i2.Height, i1.Height, i2.Height)

        Dim g As Graphics = Graphics.FromImage(i1)

        Dim cm As New Imaging.ColorMatrix(New Single()() { _
                             New Single() {1, 0, 0, 0, 0}, _
                             New Single() {0, 1, 0, 0, 0}, _
                             New Single() {0, 0, 1, 0, 0}, _
                             New Single() {0, 0, 0, zFadeAlpha, 0}, _
                             New Single() {0, 0, 0, 0, 1}})
        Dim ia As New Imaging.ImageAttributes
        ia.SetColorMatrix(cm, Imaging.ColorMatrixFlag.Default, Imaging.ColorAdjustType.Bitmap)
        g.DrawImage(i2, New Rectangle(0, 0, x + zOffset, y) _
               , 0, 0, x, y, GraphicsUnit.Pixel, ia)
        g.Dispose()

        zFadeImage = i1
        If zFadeAlpha = 1 Then zFadeImage = zFadeImg2
        If zFadeAlpha = 0 Then zFadeImage = zFadeImg1
    End Sub

    Private Sub uRibbon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.BackgroundImage = picC.BackgroundImage
    End Sub
End Class
